package com.zzy.util;
import java.io.IOException;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.FileReader;
import com.zzy.com.google.gson.Gson;
import com.zzy.card.creditCard;
import com.zzy.card.CardCatalog;
import com.zzy.factory.AmExFactory;
import com.zzy.factory.MasterFactory;
import com.zzy.factory.VisaFactory;
public class util {
	//信用卡的hashmap
    private HashMap<Integer, creditCard> hashMap = new HashMap<Integer, creditCard>();
    private HashMap<Integer, String> hashMap2 = new HashMap<Integer, String>();
    String answer;
    public String getAnswer() {
        return answer;
    }
    //单例
    private AmExFactory amExFactory = AmExFactory.getFactory();
    private MasterFactory masterFactory = MasterFactory.getFactory();
    private VisaFactory visaFactory = VisaFactory.getFactory();
    // 设置单例
    private static util util = null;
    private util() {
        hashMap.put(1, amExFactory.build2());
        hashMap.put(2, masterFactory.build2());
        hashMap.put(3, visaFactory.build2());
        hashMap2.put(1, "该卡为AmExCard");
        hashMap2.put(2, "该卡为MasterCard");
        hashMap2.put(3, "该卡为VisaCard");
    }
    public static util getUtil() {
        if (util == null) {
            util = new util();
        }
        return util;
    }
    private boolean flag1 = false; // 用来判断是否为某银行的
    private int flag2 = 0; // 用来创建卡
    public boolean isFlag() {
        return flag1;
    }
    private int Flag1 = 1;
    public void getJudge(String string) {
        flag1 = false;
        if (hashMap.get(Flag1).judge(string) == true) {
            answer = hashMap2.get(Flag1);
            flag2 = Flag1;
            Flag1 = 1;
            flag1 = true;
        } else {
            Flag1++;
            if (Flag1 <= 3) {
                getJudge(string);
            } else {
                Flag1 = 1;
                flag1 = false;
            }
        }
    }
    public void FileReader(String Filename) throws IOException {
        String filename = Filename;
        Gson gson = new Gson();
        FileReader reader = new FileReader(filename);
        BufferedReader buffer = new BufferedReader(reader);
        String line = buffer.readLine();
        while (line != null) {
            creditCard shortdata = gson.fromJson(line, creditCard.class);
            getJudge(shortdata.getCardnode());
            if (this.flag1 == true) {
                if (flag2 == 1) {
                    CardCatalog.getCatalog().getCardcatalog().add(amExFactory.build(line));
                } else if (flag2 == 2) {
                    CardCatalog.getCatalog().getCardcatalog().add(masterFactory.build(line));
                } else if (flag2 == 3) {
                    CardCatalog.getCatalog().getCardcatalog().add(visaFactory.build(line));
                }
            }
            line = buffer.readLine();
        }
        buffer.close();
    }
    public creditCard findCard(String string) {
        if (CardCatalog.getCatalog().gCreditCard1(string) != null) {
            return CardCatalog.getCatalog().gCreditCard1(string);
        } else if (CardCatalog.getCatalog().gCreditCard2(string) != null) {
            return CardCatalog.getCatalog().gCreditCard2(string);
        } else {
            return null;
        }
    }


}
